package com.me.components.pop {
	import com.me.components.fix.MapResourceLayer;
	import com.me.utils.FileUtils;
	import com.me.utils.StringUtils;

	import flash.events.MouseEvent;
	import flash.filesystem.File;

	import mx.controls.Alert;
	import mx.events.CloseEvent;

	import spark.components.Button;
	import spark.components.Group;
	import spark.components.Label;
	import spark.components.TextInput;
	import spark.components.TitleWindow;

	/**
	 *
	 * @author yw
	 *
	 * @Date 2011-9-19 下午05:37:38
	 */
	public class FileNameInput extends TitleWindow {
		private static var _instance:FileNameInput;

		private var _fileinput:TextInput;

		private var _idInput:TextInput;

		private var _nameInput:TextInput;

		public static function getInstance():FileNameInput {
			if (_instance == null) {
				_instance = new FileNameInput(new SingletonEnforce());
			}
			return _instance;
		}

		public function FileNameInput(enforce:SingletonEnforce) {
			if (enforce == null) {
				throw new Error("FileNameInput can not be intance");
			}
			width = 260;
			height = 170;
		}

		override protected function createChildren():void {
			super.createChildren();
			var l:Label = new Label();
			l.move(34, 31);
			l.text = "文件名";
			addElement(l);

			_fileinput = new TextInput();
			_fileinput.move(83, 25);
			addElement(_fileinput);

			l = new Label();
			l.move(34, 56);
			l.text = "地图ID";
			addElement(l);

			_idInput = new TextInput();
			_idInput.move(83, 50);
			_idInput.restrict = "0-9";
			addElement(_idInput);

			l = new Label();
			l.move(34, 81);
			l.text = "地图名称";
			addElement(l);

			_nameInput = new TextInput();
			_nameInput.move(83, 75);
			addElement(_nameInput);


			var btn:Button = new Button();
			btn.move(30, 105);
			btn.label = "确定";
			btn.name = "confirm";
			addElement(btn);
			btn.addEventListener(MouseEvent.CLICK, clickHandler);

			btn = new Button();
			btn.move(138, 105);
			btn.label = "取消";
			btn.name = "cancel";
			addElement(btn);
			btn.addEventListener(MouseEvent.CLICK, clickHandler);
		}

		override protected function childrenCreated():void {
			super.childrenCreated();
			title = "保存地图";
			addEventListener(CloseEvent.CLOSE, closeHandler);
		}

		private function clickHandler(e:MouseEvent):void {
			var fname:String = _fileinput.text;
			if (StringUtils.isEmpty(fname) || StringUtils.isEmpty(_idInput.text) || StringUtils.isEmpty(_nameInput.text)) {
				return;
			}
			if (e.currentTarget.name == "confirm") {
				var xml:XML = MapResourceLayer.getInstance().generateXml(_idInput.text, _nameInput.text);
				fname = File.applicationDirectory.nativePath + "/xml/" + fname + ".xml";
				FileUtils.save(fname, xml);
				Alert.show("地图文件保存成功[" + fname + "]");
			}
			dispatchEvent(new CloseEvent(CloseEvent.CLOSE));
		}

		private function closeHandler(e:CloseEvent):void {
			(parent as Group).removeElement(this);
		}

		public function locate():void {
			x = (parent.width - width) / 2;
			y = (parent.height - height) / 2;
		}
	}
}

class SingletonEnforce {

}